const BasicLayout = () => import("@/components/layout/BasicLayout");

const constantRouterComponents = {
  /* 文章 类别 */
  'CategoryInfo': () => import('@/views/category/CategoryInfo'),
  'CategoryEdit': () => import('@/views/category/CategoryEdit'),

  /* 文章 */
  'ArticleInfo': () => import('@/views/article/ArticleInfo'),
  'ArticleEdit': () => import('@/views/article/ArticleEdit')

}

export const staticRouter = {
  loginRouter: [
    {
      path: "/login",
      meta: {
        title: '登陆'
      },
      component: () => import('@/views/login/Login')
    },
  ],
  defaultRouter: [
    {
      path: "/article/detail/:id",
      component: () => import('@/views/home/HomeArticleDetail')
    }, {
      path: "/:username",
      meta: {
        title: '用户首页'
      },
      component: () => import('@/views/home/HomeIndex')
    }
  ],
  errorRouter: [
    {
      path: '/403',
      meta: {
        auth: false
      },
      component: () => import('@/views/exception/403')
    }, {
      path: '/404',
      meta: {
        auth: false
      },
      component: () => import('@/views/exception/404')
    }, {
      path: '/500',
      meta: {
        auth: false
      },
      component: () => import('@/views/exception/500')
    }, {
      path: '*', redirect: '/404',
    }
  ]
}

const rootRouter = {
  path: '/',
  component: BasicLayout,
  children: []
}

export const generatorDynamicRouter = (routerList) => {
  return new Promise((resolve, reject) => {
    try {
      rootRouter.children = generator(routerList);
      resolve(rootRouter)
    } catch (err) {
      reject(err)
    }
  })
}

/**
 * 格式化树形结构数据 生成 vue-router 层级路由表
 *
 * @param routerList
 * @returns {*}
 */
const generator = (routerList) => {
  return routerList.map(item => {
    const currentRouter = {
      // 如果路由设置了 path，则作为默认 path，否则 路由地址 动态拼接生成如 /dashboard/workplace
      path: item.path || `${parent && parent.path || ''}/${item.key}`,
      // 路由名称，建议唯一
      name: item.name || '',
      // 该路由对应页面的 组件 :方案1
      // component: constantRouterComponents[item.component || item.name],
      // 该路由对应页面的 组件 :方案2 (动态加载)
      component: (constantRouterComponents[item.name]) || (() => import(`@/views/${item.component}`)),
      // meta: 页面标题, 菜单图标, 页面权限(供指令权限用，可去掉)
      meta: {
        title: item.title,
        icon: item.icon || undefined,
        target: item.target,
      }
    }
    // 为了防止出现后端返回结果不规范，处理有可能出现拼接出两个 反斜杠
    if (!currentRouter.path.startsWith('http')) {
      currentRouter.path = currentRouter.path.replace('//', '/')
    }
    // 是否有子菜单，并递归处理
    if (item.children && item.children.length > 0) {
      // Recursion
      currentRouter.children = generator(item.children, currentRouter)
    }
    return currentRouter
  })
}

// export default {
//   name: [
//     {
//       path: "/",
//       component: BasicLayout,
//       redirect: "/category",
//       children: [
//         {
//           name: 'CategoryInfo',
//           path: '/category',
//           meta: {
//             title: "文章分类 - 列表"
//           },
//           component: () => import('@/views/category/CategoryInfo')
//         }, {
//           path: "/category/add",
//           name: "CategoryAdd",
//           meta: {
//             parent: "/category",
//             title: "文章分类 - 添加"
//           },
//           component: () => import('@/views/category/CategoryEdit')
//         }, {
//           path: "/category/edit/:id",
//           name: "CategoryEdit",
//           meta: {
//             parent: "/category",
//             title: "文章分类 - 编辑"
//           },
//           component: () => import('@/views/category/CategoryEdit')
//         }, {
//           name: 'ArticleInfo',
//           path: '/article',
//           meta: {
//             title: "文章管理 - 列表"
//           },
//           component: () => import('@/views/article/ArticleInfo')
//         }, {
//           name: 'ArticleAdd',
//           path: '/article/add',
//           meta: {
//             parent: "/article",
//             title: "文章管理 -添加"
//           },
//           component: () => import('@/views/article/ArticleEdit')
//         }, {
//           name: 'ArticleEdit',
//           path: '/article/edit/:id',
//           meta: {
//             parent: "/article",
//             title: "文章管理 -编辑"
//           },
//           component: () => import('@/views/article/ArticleEdit')
//         }
//       ]
//     }, {
//       path: '/403',
//       meta: {
//         auth: false
//       },
//       component: () => import('@/views/exception/403')
//     }, {
//       path: '/404',
//       meta: {
//         auth: false
//       },
//       component: () => import('@/views/exception/404')
//     }, {
//       path: '/500',
//       meta: {
//         auth: false
//       },
//       component: () => import('@/views/exception/500')
//     }, {
//       path: '*', redirect: '/404',
//     }
//   ]
// }
